package ca.cbc.medialib;

import com.theplatform.data.api.client.ClientConfiguration;
import com.theplatform.data.api.marshalling.PayloadForm;
import com.theplatform.media.api.client.CategoryClient;
import com.theplatform.media.api.client.MediaClient;
import com.theplatform.media.api.client.MediaFileClient;
import com.theplatform.media.api.client.ReleaseClient;
import com.theplatform.module.authentication.client.AuthenticationClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.net.UnknownHostException;
import java.rmi.RemoteException;
import java.util.Properties;

/**
 * @author Arash Bizhanzadeh <arash.bizhan-zadeh@cbc.ca>
 *         Date: 31/12/10
 *         Time: 12:34 PM
 *         Copyrighted to CBC. All rights reserved.
 */
public enum Bootstrap {
    instance;
    private Logger logger = LoggerFactory.getLogger(Bootstrap.class);

    private final MediaClient mediaClient;
    private final ReleaseClient releaseClient;
    private final CategoryClient categoryClient;
    private final MediaFileClient mediaFileClient;
    //private final NotificationClient notificationClient;

    private final String nameSpace;

    private final String uriPrefix = "http://mps.theplatform.com/data/";
    private final String username;
    private final String password;
    private final String accountId;

    public AuthenticationClient getAuthenticationClient() {
    	AuthenticationClient authenticationClient = new AuthenticationClient(
                "https://identity.auth.theplatform.com/idm",
                "mps/" + username, password, new String[]{accountId});
    	authenticationClient.setIdleTimeout(60000L);
    	logger.debug("account information is username: {}, password: {}, accountId : " + accountId, username, password);
    	return authenticationClient;
    }
    
    private Bootstrap() {
        Properties p = new Properties();
        InputStream is = null;
        try {
            is = Bootstrap.class.getClassLoader().getResourceAsStream("msb.properties");
            if (is == null) {
                is = Bootstrap.class.getResourceAsStream("msb.properties");
            }
            if (is == null) {
                throw new RemoteException("Could not find msb.properties");
            }
            p.load(is);
        } catch (IOException e) {
            logger.error("Could not find tp.property resource file");
            throw new RuntimeException("Could not find property file tp.properties", e);
        } finally {

            try {
                is.close();
            } catch (IOException e) {
                logger.error("Could not close msb.properties", e);
            }
        }
        username = p.getProperty("thePlatform.username");
        password = p.getProperty("thePlatform.password");
        accountId = p.getProperty("thePlatform.account");
        
        nameSpace = p.getProperty("custom.field.prefix");


        // construct an authentication client with credentials
        AuthenticationClient authClient = getAuthenticationClient();

        // configure the token to expire after 1 minute
        //authClient.setTokenDuration(60000);
        authClient.setIdleTimeout(3600000L);
        //authClient.setIdleTimeout(10000L);

        // set up the media client configuration.  this can only be set when the
        // client is created
        ClientConfiguration config = new ClientConfiguration();

        // make the client stream results from get() calls,
        // instead of buffering the objects in memory
        config.setStreamingResults(true);

        // payload form can be Atom, RSS, or JSON
        config.setPayloadForm(PayloadForm.JSON);

        // construct the Data Service client using the authentication client
        try {
            mediaClient = new MediaClient("http://mps.theplatform.com", authClient, config);
            releaseClient = new ReleaseClient("http://mps.theplatform.com", authClient, config);
            categoryClient = new CategoryClient("http://mps.theplatform.com", authClient, config);
            mediaFileClient = new MediaFileClient("http://mps.theplatform.com", authClient, config);
            //notificationClient = new NotificationClient(mediaClient.getBaseUrl(), mediaClient.getAuthorization(), accountId);

            //authClient.signOutToken();
        } catch (UnknownHostException e) {
            logger.error("Could not reach to thePlatform");
            throw new RuntimeException("Could not connect to thePlatform");
        }
    }

    public MediaClient getMediaClient() {
        return mediaClient;
    }

    public ReleaseClient getReleaseClient() {
        return releaseClient;
    }

    public CategoryClient getCategoryClient() {
        return categoryClient;
    }

    public MediaFileClient getMediaFileClient() {
        return mediaFileClient;
    }
    
//    public NotificationClient getNotificationClient() {
//        return notificationClient;
//    }

    public String getNameSpace() {
        return nameSpace;
    }

    public String getUriPrefix() {
        return uriPrefix;
    }
}
